<?php
class Shop_Model_Setshopmap extends Apm_Model_Base
{
	/**
	* メール送信
	*
	* @access	public
	* @param	array
	* @return	void
	*/
	public function sendMail($conig,$params,$data)
	{
		$toAddress = Apm_Config_Shop::MAIL_TENPO_MAP;
		$fromAddress = Apm_Config_Shop::MAIL_TENPO_MAP;

		// モバイルサイトのURL
		$url = $conig['apm']['baseurl'].Apm_Config_Shop::URL_DETAIL."?btn=1&UseKbn=0&ShopCd=".$params['ShopCd'];

		$subject = "店舗詳細用地図　設定完了[".$data["tenpoData"]['M05_TENPO_NAME']."]";

		$body = $data["tenpoData"]['M05_TENPO_NAME']."の店舗詳細地図の設定が完了いたしました。" . PHP_EOL.PHP_EOL;
		$body .= "下記のアドレスで設定内容を確認することが出来ます。" . PHP_EOL;
		$body .= $url;

		// メール送信処理
		$success = Apm_Otheritems::SendMail($toAddress, $fromAddress, $subject, $body);
	}
	/**
	* パラメータ作成
	*
	* @access	public
	* @param	array
	* @return	void
	*/
	public function setMapParam(&$data,$params)
	{
		// 外部モデル呼び出し設定
		$loader = new Zend_Application_Module_Autoloader(array(
			'namespace' => 'Rstdtl',
			'basePath' => APPLICATION_PATH . '/modules/rstdtl',
		));
		// 物件詳細Baseモデル生成
		$this->_model_rstdtl_base = new Rstdtl_Model_Base();

		$data['ShopCd'] = $params['ShopCd'];// 店舗CD
		$data['iPosX'] = $params['x'];		// 経度
		$data['iPosY'] = $params['y'];		// 緯度
		$data['iScrWid'] = $this->_model_rstdtl_base->changeValue($params['W'],270);	// 地図画像(横幅)
		$data['iScrHgt'] = $this->_model_rstdtl_base->changeValue($params['H'],270);	// 地図画像(縦幅)
		$data['iScrAct'] = $this->_model_rstdtl_base->changeValue($params['clk'],2);	// 地図動作 1:ｸﾘｯｸ移動、2:ﾄﾞﾗｯｸﾞｽｸﾛｰﾙ
		$data['iSetFlg'] = $this->_model_rstdtl_base->changeValue($params['set'],0);	// 0:確認1:更新

		// 地図縮尺設定
		if(is_numeric($params['scl']))
		{
			if($params['scl'] < 250)
			{
				$data['iScrCur'] = "500";
			}
			else if( 200000 < $params['scl'])
			{
				$data['iScrCur'] = "200000";
			}
			else{
				$data['iScrCur'] = $params['scl'];
			}
		}
		else
		{
			$data['iScrCur'] = 250;
		}

		// 地図URL
		$data['strSrc'] = Apm_Config_Shop::URL_JYUSHO_MAP . "&W=" .$data['iScrWid']."&H=".$data['iScrHgt'].
						"&SCL=".$data['iScrCur']."&CLK=".$data['iScrAct']."&X=".$data['iPosX']."&Y=".$data['iPosY']."&TAB=A_STA2";
	}

	/**
	* 座標取得
	*
	* @access	public
	* @param	array
	* @return	void
	*/
	public function getDefaultPoint($params)
	{
		$db_params = array();

		$sql = "";
		$sql .= $this->getDefaultPointSql($params, $db_params);

		return $this->dbGetData($sql, $db_params);
	}

	/**
	* 住所・店舗名取得
	*
	* @access	public
	* @param	array
	* @return	void
	*/
	public function getAddressDsp($params)
	{
		$db_params = array();

		$sql = "";
		$sql .= $this->getAddressDspSql($params, $db_params);

		return $this->dbGetData($sql, $db_params);
	}

	/**
	* 座標設定情報取得
	*
	* @access	public
	* @param	array
	* @return	void
	*/
	public function getServieceFlg($params)
	{
		$db_params = array();

		$sql = "";
		$sql .= $this->getServieceFlgSql($params, $db_params);

		$data =  $this->dbGetData($sql, $db_params);

		return $data['M05_SERVICE_FLAG'];
	}

	/**
	* 座標情報更新
	*
	* @access	public
	* @param	array
	* @return	void
	*/
	public function setCustomPoint($params)
	{
		$db = $this->getDbConnection();
		// トランザクションStart
		$this->startTransaction($db);

		try {

			// 店舗座標情報更新
			$this->updLocationSql($params, $db);

			// 座標設定済みフラグ更新
			$this->updTenpoServiceSql($params, $db);

			// コミット
			$this->commitTransaction($db);
		}catch (Exception $e) {
			// ロールバック
			$this->rollbackTransaction($db);
			// Bのエラーメッセージをログに書き出す
			__log_debug(__FILE__, __LINE__, __METHOD__, $e->getMessage());
			exit("ロールバック");
		}

		// トランザクションEnd
		$db->closeConnection();
	}

	/**
	* 座標取得SQL
	*
	* @access	public
	* @param	array
	* @param	array
	* @return	void
	*/
	public function getDefaultPointSql($params,&$db_params)
	{
		$db_params['strShopCd'] = $params['ShopCd'];

		$sql = "";
		$sql .= "SELECT";
		$sql .= "        M05_TENPO_IDO/1000 AS M05_TENPO_IDO";
		$sql .= "        ,M05_TENPO_KEIDO/1000 AS M05_TENPO_KEIDO";
		$sql .= "    FROM";
		$sql .= "        M05TENPO_INFO";
		$sql .= "    WHERE";
		$sql .= "        M05_TENPO_CD = : strShopCd ";

		return $sql;
	}

	/**
	* 住所・店舗名取得SQL
	*
	* @access	public
	* @param	array
	* @param	array
	* @return	void
	*/
	public function getAddressDspSql($params,&$db_params)
	{
		$db_params['strShopCd'] = $params['ShopCd'];

		$sql = "";
		$sql .= "SELECT";
		$sql .= "        M05_TENPO_NAME";
		$sql .= "        ,M05_JYUUSHO_TEXT";
		$sql .= "    FROM";
		$sql .= "        M05TENPO_INFO";
		$sql .= "    WHERE";
		$sql .= "        M05_TENPO_CD = : strShopCd ";

		return $sql;
	}

	/**
	* 座標設定情報取得SQL
	*
	* @access	public
	* @param	array
	* @param	array
	* @return	void
	*/
	public function getServieceFlgSql($params,&$db_params)
	{
		$db_params['strShopCd'] = $params['ShopCd'];

		$sql = "";
		$sql .= "SELECT";
		$sql .= "        NVL(SUBSTR(M05_SERVICE_FLAG, 13, 1),0) AS M05_SERVICE_FLAG";
		$sql .= "    FROM";
		$sql .= "        M05TENPO_SERVICE";
		$sql .= "    WHERE";
		$sql .= "        M05_TENPO_CD = : strShopCd ";

		return $sql;
	}


	/**
	* 店舗座標情報更新
	*
	* @access	public
	* @param	array
	* @param	array
	* @return	void
	*/
	public function updLocationSql($params,$db)
	{
		$db_params['strShopCd'] = $params['ShopCd'];	// 店舗コード

		$sql = "";
		$sql .= "UPDATE";
		$sql .= "        M05TENPO_INFO";
		$sql .= "    SET";
		$sql .= "        M05_LOCATION = SDO_GEOMETRY(";
		$sql .= "            2001";
		$sql .= "            ,8301";
		$sql .= "            ,SDO_POINT_TYPE(";
		$sql .= "                ".$params["x"]."* 1000 / 3600000";
		$sql .= "                ,".$params["y"]."* 1000  / 3600000";
		$sql .= "                ,NULL";
		$sql .= "            )";
		$sql .= "            ,NULL";
		$sql .= "            ,NULL";
		$sql .= "        )";
		$sql .= "        ,M05_TENPO_IDO = ".$params["y"]."*1000";
		$sql .= "        ,M05_TENPO_KEIDO = ".$params["x"]."*1000";
		$sql .= "    WHERE";
		$sql .= "        M05_TENPO_CD = : strShopCd ";

		$this->dbExecQuery($sql, $db_params,$db);
	}

	/**
	* 座標設定済みフラグ更新
	*
	* @access	public
	* @param	array
	* @param	array
	* @return	void
	*/
	public function updTenpoServiceSql($params,$db)
	{
		$db_params['strShopCd'] = $params['ShopCd'];	// 店舗コード

		$sql = "";
		$sql .= "UPDATE";
		$sql .= "        M05TENPO_SERVICE";
		$sql .= "    SET";
		$sql .= "        M05_SERVICE_FLAG = SUBSTR(M05_SERVICE_FLAG, 1, 12) || 1 || SUBSTR(M05_SERVICE_FLAG, 14)";
		$sql .= "    WHERE";
		$sql .= "        M05_TENPO_CD = : strShopCd ";

		$this->dbExecQuery($sql, $db_params,$db);
	}
}
?>